﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlTypes;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp10
{
    public class Order
    {
        [Key]
        public int IDinTab { get; set; }
        [Required]
        private Customer customer;
        private List<OrderDetails> orderDetails;
        public List<OrderDetails> OrderDetails { get; set; }
        public string Customer { get; set; }
        public double cost { get => GetCost(); }
        public int id { get; set; }
        public Order()
        {
            id = -1;
            orderDetails = new List<OrderDetails>();
        }
        public Order (int id,Customer c, List<OrderDetails> orderDetail)
        {
            this.id = id;
            customer = c;
            Customer = c.Name;
            orderDetails = orderDetail;
        }
        private double GetCost()
        {
            double c = 0;
            for(int i=0;i< orderDetails.Count;i++)
            {
                c += orderDetails[i].GetCost();
            }
            return c;
        }
        public void Add(Goods g,int n)
        {
            int length = orderDetails.Count;
            OrderDetails newOne = new OrderDetails(g, n);
            for(int i=0;i<length;i++)
            {
                if (newOne.Equals(orderDetails[i]))
                    break;
                else if (i == length - 1)
                {
                    orderDetails.Add(newOne);
                    OrderDetails.Add(newOne);
                }
            }
        }
        public void EditDetails(string n, int m)
        {
            for (int i = 0; i < orderDetails.Count; i++)
            {
                if (orderDetails[i].GetGoods().name == n && m == 0)
                {
                    orderDetails.Remove(orderDetails[i]);
                }
                else if (orderDetails[i].GetGoods().name == n)
                    orderDetails[i].SetNum(m);
                else if (i == orderDetails.Count - 1) throw new Exception("未找到相关商品");
                else continue;
                break;
            }
        }
        public OrderDetails GetDetails(int i)
        {
            return orderDetails[i];
        }
        public int GetnumofDetails()
        {
            return orderDetails.Count;
        }
        public void DelDetails(int i)
        {
            using (var db = new OrderingContext())
            {
                int id = GetDetails(i).ID;
                var details = db.Details.SingleOrDefault(b => b.ID == id);
                if (details != null)
                {
                    db.Details.Remove(details);
                    db.SaveChanges();
                }
            }
            orderDetails.Remove(orderDetails[i]);
        }
        public override bool Equals(object obj)
        {
            Order m = obj as Order;
            return m != null && m.customer == customer && m.orderDetails == orderDetails && m.cost == cost;
        }
        public override string ToString()
        {
            string str = "订单号：" + id + "\n" + Customer + "\n" + "订单内容：\n";
            using (var db = new OrderingContext())
            {
                foreach (var orde in db.Details.Where(b=>b.Order.IDinTab==this.IDinTab))
                {
                    str = str + orde.ToString();
                }
            }
            return str;
        }
        public bool FindGoods(string name)
        {
            var query = from s in orderDetails where name == s.GetGoods().name select s;
            return query.Count() == 0;
        }
        public void Swap(Order o)
        {
            Order m = new Order();
            m.id = this.id;
            m.customer = this.customer;
            m.orderDetails = this.orderDetails;
            this.id = o.id;
            this.customer = o.customer;
            this.orderDetails = o.orderDetails;
            o.id = m.id;
            o.customer = m.customer;
            o.orderDetails = m.orderDetails;
        }
    }
    public class Customer
    {
        private string name;
        public string Name { get => name; }
        public Customer(string name)
        {
            this.name = name;
        }
        public override string ToString()
        {
            return "下单者：" + name;
        }
    }
}
